package com.ssm.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: Cat.Sir
 * Description:
 * DateTime: 2022-04-30 21:16
 */
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        //如果是登录界面也放行
        if (request.getRequestURI().contains("/login")) {
            return true;
        }
        //用户登录过后
        if (session.getAttribute("uName") != null) {
            return true;
        }
        if (request.getRequestURI().contains("/register")) {
            return true;
        }

        if (request.getRequestURI().contains("/check")) {
            return true;
        }
        if (request.getRequestURI().contains("/findPwd")) {
            return true;
        }
        if (request.getRequestURI().contains("/userInfo")) {
            return true;
        }

        //request.getRequestDispatcher("/index.jsp").forward(request, response);
        //当返回首页时，用户没登陆就通过重定向跳转到登录页面
        response.sendRedirect("/ssmbuild_war_exploded/index.jsp");
        return false;
    }
}
